﻿using System;
using OpenTK;
using OpenTK.Graphics.OpenGL4;

namespace Painter3D.RenderSys
{
    class FrameBuffer : IDisposable
    {
        int handler;

        public FrameBuffer()
        {
            handler = GL.GenFramebuffer();
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, handler);

            //GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D,texture, 0);

            if (GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer) != FramebufferErrorCode.FramebufferComplete)
            {
                throw new Exception("帧缓冲创建失败");
            }
        }

        public void Use()
        {
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, handler);
        }

        static public void UseDefault()
        {
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
        }

        public void Dispose()
        {
            GL.DeleteFramebuffer(handler);
        }
    }
}
